﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.IotInfo;
using LJX.SmartPark.Domain.Student;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.IotInfo
{
    public class OledScreenLogService : IOledScreenLogService
    {
        private readonly IRepository<OledScreenLog> _studentService;

        public OledScreenLogService(IRepository<OledScreenLog> studentService)
        {
            _studentService = studentService;
        }

        public async Task<Response<bool>> DeleteByIdAsync(OledScreenLog input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<OledScreenLog>());
            return Response<bool>.Ok(result > 0);
        }

        public async Task<Response<List<OledScreenLog>>> GetOledScreenLogAsync(formInline form)
        {
            var students = await _studentService.GetAllListAsync();
            if (!string.IsNullOrEmpty(form.equipMentNum))
            {
                students = students.Where(x => x.EquipMentNum.Contains(form.equipMentNum)).ToList();
            }
            if (form.connectionStatus != 0)
            {
                if (form.connectionStatus == 1)
                {
                    students = students.Where(x => x.ConnectionStatus == true).ToList();
                }
                else
                {
                    students = students.Where(x => x.ConnectionStatus == false).ToList();
                }
            }
            var count = students.Count();
            students = students.Skip((form.pageIndex - 1) * form.pageSize).Take(form.pageSize).ToList();
            var result = students.MapToList<OledScreenLog>();
            return Response<List<OledScreenLog>>.Ok(result, count);
        }

        public Response<OledScreenLog> GetOledScreenLogById(OledScreenLog input)
        {
            var student = _studentService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = student.MapTo<OledScreenLog>();
            return Response<OledScreenLog>.Ok(result);
        }

        public async Task<Response<bool>> PostAddOledScreenLog(OledScreenLog input)
        {
            input.CreateDate = DateTime.Now;
            var result = _studentService.InsertAsyncById(input.MapTo<OledScreenLog>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> PutUpdateOledScreenLog(OledScreenLog input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<OledScreenLog>());

            return Response<bool>.Ok(result.Result > 0);
        }
    }
}
